var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (e) {
  return typeof e;
} : function (e) {
  return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
},
    _createClass = function () {
  function n(e, t) {
    for (var r = 0; r < t.length; r++) {
      var n = t[r];
      n.enumerable = n.enumerable || !1, n.configurable = !0, "value" in n && (n.writable = !0), Object.defineProperty(e, n.key, n);
    }
  }

  return function (e, t, r) {
    return t && n(e.prototype, t), r && n(e, r), e;
  };
}();

function _classCallCheck(e, t) {
  if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function");
}

var towxml = function () {
  function i(e) {
    _classCallCheck(this, i);

    var n = this;

    for (var t in e = e || {}) n.config[t] = e[t];

    n.m = {};
    var r = {
      html: !0,
      xhtmlOut: !0,
      typographer: !0,
      highlight: function (e, t, r) {
        return n.m.highlight.highlightAuto(e).value;
      }
    };
    global ? (n.m.html2json = require("./lib/html2json.js"), n.m.highlight = require("./plugins/hljs/index.js"), n.m.md = require("./lib/markdown-it.js")(r), n.m.md_sub = require("./plugins/markdown-it-sub.js"), n.m.md_sup = require("./plugins/markdown-it-sup.js"), n.m.md_ins = require("./plugins/markdown-it-ins.js"), n.m.md_mark = require("./plugins/markdown-it-mark.js")) : window && (n.m.html2json = window.html2json, n.m.highlight = window.hljs, n.m.md = new window.markdownit(r), n.m.md_sub = window.markdownitSub, n.m.md_sup = window.markdownitSup, n.m.md_ins = window.markdownitIns, n.m.md_mark = window.markdownitMark), n.m.md.use(n.m.md_sub), n.m.md.use(n.m.md_sup), n.m.md.use(n.m.md_ins), n.m.md.use(n.m.md_mark), n.wxmlTag = ["view", "video", "swiper", "block", "swiper-item", "button", "slider", "scroll-view", "movable-area", "movable-view", "text", "progress", "checkbox-group", "label", "checkbox", "form", "switch", "input", "radio-group", "radio", "picker", "picker-view", "switch", "textarea", "navigator", "audio", "image", "map", "canvas", "contact-button"];
  }

  return _createClass(i, [{
    key: "md2html",
    value: function (e) {
      return this.m.md.render(e);
    }
  }, {
    key: "html2wxml",
    value: function (e) {
      var l = this;
      return e.replace(/<[^<]*>/gi, function (e) {
        var r;

        if ("</" === e.substr(0, 2)) {
          var t = e.substr(2, e.length - 3).toLowerCase();
          if (l.isConversion(t)) return "</" + l.newLabel(t) + ">";
        } else {
          var n = e.substr(1, e.length - 2).split(" "),
              i = n[0].toLowerCase(),
              o = "h2w__" + i;

          if (l.isConversion(i)) {
            n.splice(0, 1);
            var a = n.length;
            (function () {
              if (a) for (var e = 0; e < a; e++) {
                var t = n[e],
                    r = /class="/gi;
                if (r.test(t)) return n[e] = t.replace(r, function (e) {
                  return e + o + " ";
                }), !0;
              }
              return !1;
            })() || n.unshift('class="' + o + '"');
            var s = (r = "", n.forEach(function (e, t) {
              r += e + " ";
            }), r = r.substr(0, r.length - 1));
            return "img" === i ? "<image " + s + "></image>" : "<" + l.newLabel(i) + " " + s + ">" + l.needClose(i);
          }
        }

        return e;
      });
    }
  }, {
    key: "md2wxml",
    value: function (e) {
      var t,
          r,
          n = this.md2html(e),
          i = this.html2wxml(n);
      return r = "", 0 === (t = i).length ? r : r = (r = (r = (r = (r = (r = t.replace(/&amp;/gi, "&")).replace(/&lt;/gi, "＜")).replace(/&gt;/gi, "＞")).replace(/&nbsp;/g, " ")).replace(/&#39;/g, "'")).replace(/&quot;/g, '"');
    }
  }, {
    key: "isConversion",
    value: function (r) {
      return !this.wxmlTag.some(function (e, t) {
        return r === e;
      });
    }
  }, {
    key: "needClose",
    value: function (r) {
      var e = "";
      return ["hr", "br"].some(function (e, t) {
        return r === e;
      }) && (e = "</view>"), e;
    }
  }, {
    key: "newLabel",
    value: function (e) {
      var t = "view";

      switch (e) {
        case "a":
          t = "navigator";
          break;

        case "span":
        case "b":
        case "strong":
        case "i":
        case "em":
        case "code":
        case "sub":
        case "sup":
        case "g-emoji":
        case "mark":
        case "ins":
          t = "text";
      }

      return t;
    }
  }, {
    key: "toJson",
    value: function (e, t) {
      var r = "",
          n = void 0;
      return "markdown" === (t = t || "html") ? r = this.m.html2json(this.md2wxml(e)) : "html" === t && (r = this.m.html2json(this.html2wxml(e))), (n = function (e) {
        for (var t in e) "child" === t && "object" === _typeof(e[t]) && e[t].length && e[t].forEach(function (e, t) {
          n(e);
        }), "attr" === t && ("string" == typeof e[t].class ? e[t].className = e[t].class : "object" === _typeof(e[t].class) && e[t].class.length && (e[t].className = e[t].class.toString().replace(/,/g, " ")));
      })(r), r.theme = "light", r;
    }
  }]), i;
}();

module.exports = towxml;